package leetcode;

import java.util.ArrayList;
import java.util.List;

public class lc228 {
    /**
     * Input: nums = [0,1,2,4,5,7]
     * Output: ["0->2","4->5","7"]
     * Explanation: The ranges are:
     * [0,2] --> "0->2"
     * [4,5] --> "4->5"
     * [7,7] --> "7"
     */
    public static void main(String[] args) {
        summaryRanges(new int[]{0,1,2,4,5,7});
    }
    public static List<String> summaryRanges(int[] nums) {
        int n = nums.length;
        int i = 0,j;
        List<String> ret = new ArrayList<>();
        for(j=i+1;j<=n;++j){
            if(j == n){
                String s = "";
                if(nums[i]!=nums[j-1]) {
                    s = nums[i] + "->" + nums[j - 1];
                }else{
                    s = nums[i]+"";
                }
                ret.add(s);

            }
            if(nums[j] - nums[j-1] != 1){
                String s = "";
                if(nums[i]!=nums[j-1]) {
                    s = nums[i] + "->" + nums[j - 1];
                }else{
                    s = nums[i]+"";
                }
                ret.add(s);
                i = j;
            }
        }
        return ret;
    }
}
